# from sklearn.cluster import KMeans
# import numpy as np
#
# # 数据点
# X = np.array([[2, 10], [2, 5], [8, 4], [5, 8], [7, 5], [6, 4], [1, 2], [4, 9]])
#
# # 创建KMeans实例，传入初始簇中心坐标,k的个数,限制最大呆迭代次数为1
# kmeans = KMeans(n_clusters=3, init=[[2, 10], [5, 8], [1, 2]])
#
# # 拟合模型
# kmeans.fit(X)
#
# # 打印结果
# print("Cluster centers:", kmeans.cluster_centers_)
# print("Labels:", kmeans.labels_)


from assignment_2.src.scripts import get_label, show_k, show_res, get_new_k, platform
from data_structures.coord import Coord

if __name__ == '__main__':
    # 初始化簇中心
    k1 = Coord(2, 10)
    k2 = Coord(5, 8)
    k3 = Coord(1, 2)
    k = [k1, k2, k3]

    # 初始化坐标
    a1 = Coord(2, 10)
    a2 = Coord(2, 5)
    a3 = Coord(8, 4)
    b1 = Coord(5, 8)
    b2 = Coord(7, 5)
    b3 = Coord(6, 4)
    c1 = Coord(1, 2)
    c2 = Coord(4, 9)
    dataSet = [a1, a2, a3, b1, b2, b3, c1, c2]

    # 初始化结果
    res = [[], [], []]
    new_k = 0
    count = 0  # 计数

    # 返回最终的聚类结果
    while new_k != 1:
        res = [[], [], []]  # 结果初始化
        for i in dataSet:
            i.label = get_label(i, k[0], k[1], k[2])
            match i.label:
                case 0:
                    res[0].append(i)
                case 1:
                    res[1].append(i)
                case 2:
                    res[2].append(i)

        new_k = get_new_k(res)
        if new_k != k:
            k = new_k
            count += 1

            print(f'第{count}轮结果打印')
            print(f"聚类结果：{show_res(res)}")
            print(f"聚类的结果（簇中心）：{show_k(new_k)}")
            platform(res, new_k)

        else:
            new_k = 1
    else:
        print(f'共迭代{count}轮')
